const app = getApp()
import axios from '../../utils/axios'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    transformX: 0,
    currentIndex: 1,
    orderList: [],
    allList: [], //全部
    paymentList: [], //待支付
    shipmentsList: [], //代发货
    putCargoList: [], // 待收货
    completeList: [] // 已完成
  },

  onLoad: function (options) {

    if (options.index) {
      this.setData({
        currentIndex: options.index
      })
    }

    this.calculateX(this.data.currentIndex)

  },

  onShow: function () {
    this.selectGoodsInfo()
  },

  // 跳转订单详情
  orderDetails: function (e) {
    console.log(e)
    wx.navigateTo({
      url: '/pages/orderDetail/orderDetail?orderId=' + e.currentTarget.dataset.orderid,
    })
  },

  selectGoodsInfo: function () {
    axios('/cloud/goodsOrders').then(res => {
      let conduct = [],
        complete = [];

      let paymentList = [], //待支付
        shipmentsList = [], //代发货
        putCargoList = [], // 待收货
        completeList = [], // 已完成
        otherList = []; //其它状态

      if (res.data.resultcode == 1) {

        let list = res.data.result

        for (let i = 0, len = list.length; i < len; i++) {

          let allCount = 0

          for (let j = 0; j < list[i].goods.length; j++) {
            allCount += parseInt(list[i].goods[j].num)
            if (list[i].goods[j].attach == false) {
              list[i].goods[j]['showMoney'] = list[i].goods[j].price
            } else {
              list[i].goods[j]['showMoney'] = list[i].goods[j].attach.discount > 0.01 ? list[i].goods[j].attach.discount : list[i].goods[j].attach.price
            }
          }

          list[i]['showCount'] = allCount

          if (list[i].pay_status == '1') {
            paymentList.push(list[i])
          } else if (list[i].status == '0' && list[i].pay_status == '2') {
            // shipmentsList.push(list[i])
            putCargoList.push(list[i])
          } else if (list[i].status == '1' && list[i].pay_status == '2') {
            putCargoList.push(list[i])
          } else if (list[i].status == '4' || list[i].status == 6 && list[i].pay_status == '2') {
            completeList.push(list[i])
          } else {
            otherList.push(list[i])
          }

        }

        console.log("不应该有值的otherList", otherList)

        this.setData({
          allList: res.data.result,
          paymentList: paymentList,
          shipmentsList: shipmentsList,
          putCargoList: putCargoList,
          completeList: completeList
        });

        // 赋值
        this.assignment()

      }
    })
  },

  // 确认收货
  affirmPut: function (e) {
    const self = this
    wx.showModal({
      content: '确认收到货了吗？',
      confirmText: "确认",
      cancelText: "取消",
      success: function (res) {
        if (res.confirm) {
          let orderId = e.currentTarget.dataset.orderid

          axios('/cloud/finishGoodsOrder', {
            order_id: orderId
          }).then(res => {
            if (res.data.resultcode == 1) {
              wx.showToast({
                title: "收货成功！",
                icon: 'none'
              })
              self.selectGoodsInfo()
            }
          })
        }
      }
    })

  },

  // 申请退款
  refundGoods: function (e) {
    let orderId = e.currentTarget.dataset.orderid

    axios('cloud/refundGoods', {
      order_id: orderId
    }).then(res => {
      if (res.data.resultcode == 1) {
        wx.showToast({
          title: res.data.message,
          icon: 'none'
        })
        this.selectGoodsInfo()
      }
    })

  },

  // 取消订单
  cancelOrder(e) {
    let self = this
    let orderId = e.currentTarget.dataset.orderid

    wx.showModal({
      content: '确定取消该订单吗？',
      confirmText: "确认",
      cancelText: "取消",
      success: function (res) {
        if (res.confirm) {
          axios('/cloud/cancelGoodsOrder', {
            order_id: orderId
          }).then(res => {

            if (res.data.resultcode == 1) {
              wx.showToast({
                title: res.data.message,
                icon: 'none',
                duration: 1300
              })
              setTimeout(() => {
                self.selectGoodsInfo()
              }, 1000)
            } else {
              wx.showToast({
                title: res.data.message,
                icon: 'none',
                duration: 1300
              })
            }

          })
        }
      }
    })

  },

  // 付款
  goPayment: function (e) {

    const self = this
    let orderId = e.currentTarget.dataset.orderid
    axios("/cloud/goodsOrderPay", {
      order_id: orderId,
      type: 'cloud'
    }).then(res => {
      if (res.data.resultcode == 1) {
        let payment = JSON.parse(res.data.result.jsApiParameters)

        wx.requestPayment({
          timeStamp: payment.timeStamp,
          nonceStr: payment.nonceStr,
          package: payment.package,
          signType: payment.signType,
          paySign: payment.paySign,
          success: res => {
            wx.showToast({
              title: '支付成功',
              icon: 'none'
            })
            self.selectGoodsInfo()
          },
          fail: err => {
            wx.showToast({
              title: '取消支付',
              icon: 'none'
            })
          }
        })
      } else {
        wx.showToast({
          title: res.data.message,
          icon: 'none'
        })
      }
    })
  },

  // 再来一单
  recurOrder: function (e) {
    let orderId = e.currentTarget.dataset.orderid

    wx.navigateTo({
      url: '/pages/shopp/shopp',
    })

  },

  // 提醒发货
  remind: function () {
    wx.showToast({
      title: '已提醒商品发货哦！',
      icon: 'none',
      duration: 1500
    })
  },

  // 赋值
  assignment: function () {
    let index = this.data.currentIndex
    let list = []

    if (index == 1) {
      list = this.data.allList
    } else if (index == 2) {
      list = this.data.paymentList
    } else if (index == 3) {
      list = this.data.putCargoList
    } else if (index == 4) {
      list = this.data.completeList
    } else if (index == 5) {
      // list = this.data.
    }

    this.setData({
      orderList: list
    })

  },

  // 查询订单列表


  // 切换类型
  cutType: function (e) {
    console.log(e)

    let index = e.currentTarget.dataset.index

    this.setData({
      currentIndex: index
    })

    this.calculateX(index)

    this.assignment()

  },

  // 计算transformX
  calculateX: function (index) {
    const self = this
    wx.createSelectorQuery().select(".nav-li").boundingClientRect(function (res) {

      let width = parseFloat((res.width - 26) / 2)

      let transX = (res.width * index - res.width) + width

      self.setData({
        transformX: transX
      })

    }).exec()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})